package org.one.afternoon.service.impl;

import org.one.afternoon.domain.User;
import org.one.afternoon.mapper.UserMapper;
import org.one.afternoon.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User register(User user) {
        // 先去数据库找用户名是否存在
        User getUser = userMapper.findUserByUsername(user.getUsername());
        if (getUser != null) {
            //该用户名已存在!
            return null;
        }
        // 存入数据库
        userMapper.addAdminUser(user);
        return user;
    }

    @Override
    public User login(User user) {
        // 去数据库查找用户
        User getUser = userMapper.findUserByUsername(user.getUsername());
        if (getUser == null) {
            //用户不存在！
            return null;
        }
        if (!getUser.getPassword().equals(user.getPassword())) {
            //用户名或者密码错误！
            return null;
        }
        // 设定登录成功消息
        userMapper.updateOnline(1,user.getUsername());

        return getUser;
    }

    @Override
    public User logout(User user) {
        User getUser=userMapper.findUserByUsername(user.getUsername());
        userMapper.updateOnline(0,user.getUsername());
        return getUser;
    }

    @Override
    public User isLogin(User user) {
        return null;
    }


    @Override
    public User addUser(User user) {
        userMapper.addUser(user);
        //添加用户信息成功
        return user;
    }

    @Override
    public User update(User user) {
        userMapper.updateUser(user);
        //修改用户信息成功！
        return user;
    }

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    //复合查询
    @Override
    public List<User> findUser(User user) {
        return userMapper.findUser(user);
    }

    @Override
    public void delete(Integer index) {
        userMapper.deleteUserById(index);
        //删除成功!
    }

    @Override
    public Integer queryOnline() {
        return userMapper.findOnline();
    }

    @Override
    public Integer queryTotal() {
        return userMapper.findTotal();
    }

    @Override
    public Integer queryTdy(Integer time) {
        return userMapper.findTdy(time);
    }

    @Override
    public Integer queryNew(int time) {
        return userMapper.findNew(time);
    }
}
